package com.sd.common.utils;
import com.sd.common.utils.cti.IConverter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Converters {
    private static final String timeStampPattern = "yyyy-MM-dd HH:mm:ss";
    private static final String datePattern = "yyyy-MM-dd";
    private static final int dateLen = "yyyy-MM-dd".length();
    private static final int timeStampWithoutSecPatternLen = "yyyy-MM-dd HH:mm".length();
    private static final int timePatternLen = "hh:mm:ss".length();
    private static final int timeWithoutSecPatternLen = "hh:mm".length();

    private Converters() {
    }

    public static String supportHtml5DateTimePattern(String s) {
        return s.indexOf(32) == -1 && s.indexOf(84) != -1 && s.indexOf(45) != -1 && s.indexOf(58) != -1 ? s.replace("T", " ") : s;
    }

    public static class TimestampConverter implements IConverter<Timestamp> {
        public TimestampConverter() {
        }

        public Timestamp convert(String s) throws ParseException {
            s = Converters.supportHtml5DateTimePattern(s);
            if (Converters.timeStampWithoutSecPatternLen == s.length()) {
                s = s + ":00";
            }

            return s.length() > Converters.dateLen ? Timestamp.valueOf(s) : new Timestamp((new SimpleDateFormat("yyyy-MM-dd")).parse(s).getTime());
        }
    }

    public static class TimeConverter implements IConverter<Time> {
        public TimeConverter() {
        }

        public Time convert(String s) throws ParseException {
            int len = s.length();
            if (len == Converters.timeWithoutSecPatternLen) {
                s = s + ":00";
            }

            if (len > Converters.timePatternLen) {
                s = s.substring(0, Converters.timePatternLen);
            }

            return Time.valueOf(s);
        }
    }

    public static class SqlDateConverter implements IConverter<Date> {
        public SqlDateConverter() {
        }

        public Date convert(String s) throws ParseException {
            s = Converters.supportHtml5DateTimePattern(s);
            if (Converters.timeStampWithoutSecPatternLen == s.length()) {
                s = s + ":00";
            }

            return s.length() > Converters.dateLen ? new Date((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(s).getTime()) : new Date((new SimpleDateFormat("yyyy-MM-dd")).parse(s).getTime());
        }
    }

    public static class DateConverter implements IConverter<java.util.Date> {
        public DateConverter() {
        }

        public java.util.Date convert(String s) throws ParseException {
            s = Converters.supportHtml5DateTimePattern(s);
            if (Converters.timeStampWithoutSecPatternLen == s.length()) {
                s = s + ":00";
            }

            return s.length() > Converters.dateLen ? (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(s) : (new SimpleDateFormat("yyyy-MM-dd")).parse(s);
        }
    }

    public static class BooleanConverter implements IConverter<Boolean> {
        public BooleanConverter() {
        }

        public Boolean convert(String s) {
            String value = s.toLowerCase();
            if (!"true".equals(value) && !"1".equals(value)) {
                if (!"false".equals(value) && !"0".equals(value)) {
                    throw new RuntimeException("Can not parse to boolean type of value: " + s);
                } else {
                    return Boolean.FALSE;
                }
            } else {
                return Boolean.TRUE;
            }
        }
    }

    public static class BigDecimalConverter implements IConverter<BigDecimal> {
        public BigDecimalConverter() {
        }

        public BigDecimal convert(String s) {
            return new BigDecimal(s);
        }
    }

    public static class BigIntegerConverter implements IConverter<BigInteger> {
        public BigIntegerConverter() {
        }

        public BigInteger convert(String s) {
            return new BigInteger(s);
        }
    }

    public static class ByteArrayConverter implements IConverter<byte[]> {
        public ByteArrayConverter() {
        }

        public byte[] convert(String s) {
            return s.getBytes();
        }
    }

    public static class DoubleConverter implements IConverter<Double> {
        public DoubleConverter() {
        }

        public Double convert(String s) {
            return Double.parseDouble(s);
        }
    }

    public static class FloatConverter implements IConverter<Float> {
        public FloatConverter() {
        }

        public Float convert(String s) {
            return Float.parseFloat(s);
        }
    }

    public static class LongConverter implements IConverter<Long> {
        public LongConverter() {
        }

        public Long convert(String s) {
            return Long.parseLong(s);
        }
    }

    public static class ByteConverter implements IConverter<Byte> {
        public ByteConverter() {
        }

        public Byte convert(String s) {
            return Byte.parseByte(s);
        }
    }

    public static class ShortConverter implements IConverter<Short> {
        public ShortConverter() {
        }

        public Short convert(String s) {
            return Short.parseShort(s);
        }
    }

    public static class IntegerConverter implements IConverter<Integer> {
        public IntegerConverter() {
        }

        public Integer convert(String s) {
            return Integer.parseInt(s);
        }
    }
}

